import DataBus from "../databus";
import Utils from "../common/utils";
import {PageStatus} from "../page_status";
import XmlManager from "../manager/xml_manager";
import Scene from "./scene";
import Text from "../base/text";

const databus = new DataBus()
const xmlManager = new XmlManager()
const btnImg = new Image()
import {winW, winH, px, px2rem} from '../libs/utils.js';
import TextAnim from "../base/text_anmi";
import JsLogic from "./jslogic";

const __ = {
  timer: Symbol('timer'),
}

/**
 * 游戏介绍页 ui
 */
export default class GameIntroduce extends Scene{
  constructor() {
    super(0, 0, winW, winH);

    this.btnX = 0
    this.btnY = 0
    this.btnW = 0
    this.btnH = 0

    this.tvX = 0
    this.tvY = 0
    this.tvW = 0
    this.tvH = 0
    this.tvFont = ''
    this.tvContent = ""
    // 帧定时器
    this[__.timer] = null
  }

  update() {
    super.update();
    if (databus.gameStatus !== PageStatus.GameIntroduce) {

      return;
    }
    // debugger
    let imgAttribute = xmlManager.obtainAttributes(xmlManager.sceneIntroduce.component.displayList.image, "btn_loading");
    let tvAttribute = xmlManager.obtainAttributes(xmlManager.sceneIntroduce.component.displayList.text, "tv_loading");

    btnImg.src = xmlManager.assetName(imgAttribute.fileName)

    let [x, y] = imgAttribute.xy.split(",");
    let [w, h] = imgAttribute.size.split(",");
    this.btnX = x;
    this.btnY = y;
    this.btnW = w;
    this.btnH = h;

    let [xx, yy] = tvAttribute.xy.split(",");
    let [ww, hh] = tvAttribute.size.split(",");

    this.tvX = xx
    this.tvY = (parseInt(yy))
    this.tvW = ww
    this.tvH = hh

    this.tvFont = tvAttribute.fontSize.concat('px Arial')
    this.tvContent = tvAttribute.text

    /**
     * 重新开始按钮区域
     * 方便简易判断按钮点击
     */

    this.btnIntroduce = {
      startX: this.btnX,
      startY: this.btnY,
      endX: parseInt(this.btnX) + parseInt(this.btnW),
      endY: parseInt(this.btnY) + parseInt(this.btnH)
    }
  }

  render(ctx) {

    if (databus.gameStatus !== PageStatus.GameIntroduce) {
      return;
    }
    ctx.drawImage(
        btnImg,
        this.btnX, this.btnY, this.btnW, this.btnH,
    )
    super.render(ctx);

  }

  initView(ctx) {
    super.initView();
    var introduceText = new TextAnim(this.tvContent,  this.tvX, this.tvY,20 );
    // debugger
    var px2rem1 = px2rem(winW,this.tvW);

    introduceText.maxWidth =  winW-40;
    // introduceText.maxWidth = this.tvW;
    introduceText.textWrap = true;
    introduceText.color = 'red';
    this.addChild(introduceText,"introduceText")

  }

  onTouchEvent(type, x, y, callback) {
      
    switch (type) {
      case 'touchstart':
        // callback({ message: 'already consumed'})

        if (databus.gameStatus === PageStatus.GameIntroduce) {
          if (Utils.inArea({x, y}, this.btnIntroduce)) {

            var jsLogic = new JsLogic();
            if(jsLogic.isNewUser){
              databus.gameStatus = PageStatus.RouteSelect;
            }else {
              databus.gameStatus = PageStatus.GameMainMenu;
            }


            // wx.exitMiniProgram()
            // wx.authorize({
            //   scope: 'scope.userInfo',
            // })
            // wx.showShareMenu()
            // wx.shareAppMessage({
            //     title: '来跟我一起玩 旅行猫咪'
            //   })
            //   wx.onShareTimeline((result) => {})
              
            callback({message: 'start'})
          }
          console.log(`onTouchEvent： gameStatus? ${databus.gameStatus}`)
        }
        break
    }
  }
}
